
package com.hlkj.pay.listener;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.framework.caches.service.RedisTemplateService;
import com.hlkj.pay.common.constants.MerchantConstant;
import com.hlkj.pay.service.common.IBusinessLogService;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;

import com.hlkj.pay.app.finance.IFinanceOrderAppService;
import com.hlkj.pay.common.constants.MqConstant;
import com.hlkj.pay.dto.common.OrderStatusDto;

import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/24 12:58
 */
@Slf4j
@Component
@ConditionalOnProperty(prefix = "rocketmq", name = "enabled", havingValue = "true")
@RocketMQMessageListener(topic = MqConstant.FEE_CALCULATE_TOPIC, consumerGroup = MqConstant.FEE_CALCULATE_TOPIC)
public class OrderFeeCalculateConsumer implements RocketMQListener<OrderStatusDto>, RocketMQPushConsumerLifecycleListener {

    @Autowired
    private IFinanceOrderAppService financeOrderAppService;

    @Autowired
    private RedisTemplateService redisTemplateService;

    @Value("${rocketmq.consumer.consume_key_time:60}")
    private Integer consumeKeyTime;
    @Autowired
    private IBusinessLogService businessLogService;
    @Value("${server.port}")
    private String serverPort;
    @Override
    public void onMessage(OrderStatusDto orderStatusDto) {
        String lockKey = key(orderStatusDto);
        Boolean hasKey = redisTemplateService.hasKey(lockKey);
        // businessLogService.consumeOrderStatusLog(MqConstant.FEE_CALCULATE_TOPIC,orderStatusDto);
        // 5分钟
        if (!hasKey) {
            redisTemplateService.set(lockKey, orderStatusDto.getOrderNo(), consumeKeyTime);
        }
        else {
            log.error("fee_calculate consumer lock fail orderNo:{},type:{}", orderStatusDto.getOrderNo(), orderStatusDto.getOrderStatusType());
            return;
        }
        try {
            financeOrderAppService.processOrderFeeCalculate(orderStatusDto);
        }
        catch (Exception e) {
            e.printStackTrace();
            log.error("fee_calculate consumer error :{}", e, e.getMessage());
        }finally {
            redisTemplateService.del(lockKey);
        }
    }

    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        // 设置当前实例的名称
        consumer.setInstanceName(MqConstant.FEE_CALCULATE_TOPIC + "_" + serverPort);
    }

    String key(OrderStatusDto orderStatusDto) {
        return MerchantConstant.REDIS_FEE_CALCULATE + orderStatusDto.getOrderStatusType() + ":" + orderStatusDto.getOrderNo();
    }
}
